package com.zidian.teacher.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.google.gson.Gson;
import com.zidian.teacher.R;
import com.zidian.teacher.bean.ClassListBean;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import com.zidian.teacher.adapter.AttendanceTotalAdapter;
import com.zidian.teacher.bean.AttendanceTotalBean;
import com.zidian.teacher.common.Constant;
import com.zidian.teacher.common.ToolBarActivity;
import com.zidian.teacher.utils.GetUserInfo;
import com.zidian.teacher.utils.ProgressBarBlackUtil;
import com.zidian.teacher.utils.ToastUtil;

/**
 * Created by Administrator on 2016/9/11 0011.
 * 获取考勤统计
 */
public class AttendanceTotalActivity extends ToolBarActivity {

    @Bind(R.id.total_class_spinner)
    Spinner totalClassSpinner;
    @Bind(R.id.list)
    RecyclerView mRecyclerView;
    private List<String> data_list;
    private List<AttendanceTotalBean.DataBean> data = new ArrayList<>();
    private AttendanceTotalAdapter adapter;
    private ArrayAdapter<String> arr_adapter;
    private String courseId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolbarView(R.layout.toolbar_left);
        setContentView(R.layout.activity_attendance_total);
        setTitle("考勤统计");
        ButterKnife.bind(this);
        courseId = getIntent().getStringExtra("courseId");
        initView();
        getClassList();
    }

    private void initView() {
        adapter = new AttendanceTotalAdapter(context, data);
        final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(adapter);


        //添加点击事件
        adapter.setOnItemClickListener(new AttendanceTotalAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Log.d("test", "item position = " + position);
//                startActivity(InteractionAskDetailAct.class);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
        totalClassSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //选择班级，获取学生
                data.clear();
                getStudentAttendance(data_list.get(position));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }


    /**
     * 获取班级列表 用于获取考勤统计
     */
    private void getClassList() {
        String loginUrl = Constant.URL_GOAL + "attendance/selectClass";
        HashMap<String, String> map = new HashMap<>();
        map.put("courseId", courseId);
        map.put("teacherId", GetUserInfo.getTeacherId(context));
        map.put("token", GetUserInfo.getToken(context));

        ProgressBarBlackUtil.loadShow(context);

        OkHttpUtils.post().url(loginUrl).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtil.showToast(context, "获取失败！");
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e("T", response);
                ProgressBarBlackUtil.loadCancel();
                try {
                    JSONObject jsonObject = null;
                    jsonObject = new JSONObject(response);
                    int resultCode = jsonObject.getInt("code");

                    if (resultCode != 200) {
                        ToastUtil.showToast(context, jsonObject.getString("message"));
                        return;
                    }

                } catch (JSONException e) {

                }

                Gson gson = new Gson();
                ClassListBean model = gson.fromJson(response, ClassListBean.class);
                if (model.isSuccess()) {

                    //数据
                    data_list = new ArrayList<>();

                    for (int i = 0; i < model.getData().size(); i++) {
                        data_list.add(model.getData().get(i).getClassId());
                    }

                    //适配器
                    arr_adapter = new ArrayAdapter<>(context, R.layout.spinner_item, data_list);
                    //设置样式
                    arr_adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
                    //加载适配器
                    totalClassSpinner.setAdapter(arr_adapter);
//                    getStudentAttendance(data_list.get(0));

                }

            }
        });
    }


    /**
     * @param className 从服务器获取考勤信息
     */
    private void getStudentAttendance(String className) {
        String loginUrl = Constant.URL_GOAL + "attendance/selectAll";
        HashMap<String, String> map = new HashMap<>();
        map.put("courseId", courseId);
        map.put("teacherId", GetUserInfo.getTeacherId(context));
        map.put("token", GetUserInfo.getToken(context));
        map.put("className", className);

        ProgressBarBlackUtil.loadShow(context);

        OkHttpUtils.post().url(loginUrl).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtil.showToast(context, "获取失败！");
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e("T", response);
                ProgressBarBlackUtil.loadCancel();
                try {
                    JSONObject jsonObject = null;
                    jsonObject = new JSONObject(response);
                    int resultCode = jsonObject.getInt("code");

                    if (resultCode != 200) {

                        ToastUtil.showToast(context, jsonObject.getString("message"));
                        adapter.notifyDataSetChanged();
                        return;
                    }

                } catch (JSONException e) {

                }

                Gson gson = new Gson();
                AttendanceTotalBean model = gson.fromJson(response, AttendanceTotalBean.class);
                if (model.isSuccess()) {

                    data.addAll(model.getData());
                    adapter.notifyDataSetChanged();

                }

            }
        });
    }


}
